<?php
namespace Home\Controller;
use Think\Controller;
class SetController extends CommonController {
    public function index(){

    	$uid = $_SESSION['uid'];

    	// $sql="select * from user where uid='{$uid}'";
    	
    	// M()->query($sql);

    	$user = M('user')->find($uid);

    	$this->assign('user',$user);
        $this->assign('title','资料修改');
        $this->display();
    }

    public function upload()
    {
    	$upload = new \Think\Upload();// 实例化上传类
	    $upload->maxSize   =     3145728 ;// 设置附件上传大小
	    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
	    $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
	    $upload->savePath  =     ''; // 设置附件上传（子）目录
	    // 上传文件 
	    $info   =   $upload->upload();

	    if(!$info) {// 上传错误提示错误信息
	        $this->error($upload->getError());
	        echo "error";
	    }else{// 上传成功

	    	$info = current($info);

	    	$path = "Uploads/".$info['savepath'].$info['savename'];


	    	M('user')->where(['uid'=>$_SESSION['uid']])->save(['face'=>$path]);

	    	$_SESSION['face']=$path;

	    	echo "ok";
	    }
    }

    public function edit()
    {
    	$data = I('post.');

    	$uid = $_SESSION['uid'];

    	$User = D("User"); // 实例化User对象

		if (!$User->create($data,2)){ // 指定新增数据
		     // 如果创建失败 表示验证没有通过 输出错误提示信息
		     $this->ajaxReturn(['error'=>1,'info'=>$User->getError()]);
		}else{

			$res = M('user')->where(['email'=>$data['email'],'uid'=>['neq',$uid]])->find();

			if($res){
				$this->ajaxReturn(['error'=>1,'info'=>"邮箱已存在"]);
			}


			$res = M('user')->where(['nickname'=>$data['nickname'],'uid'=>['neq',$uid]])->find();

			if($res){
				$this->ajaxReturn(['error'=>1,'info'=>"昵称已存在"]);
			}


			M('user')->where(['uid'=>$uid])->save($data);


			$this->ajaxReturn(['error'=>0,'info'=>"更新成功"]);


			// $sql ="select * from user where email='120@qq.com' and uid <> $uid";


		}
    }

    public function password()
    {
    	$data = I('post.');

    	if($data['password']<>$data['repassword']){
    		$this->ajaxReturn(['error'=>1,'info'=>"两次密码不一致"]);
    	}

    	$uid = $_SESSION['uid'];


    	$user = M('user')->find($uid);

    	$password = md5($data['password']);

    	if($password==$user['password']){
    		$this->ajaxReturn(['error'=>1,'info'=>"密码跟老密码一样"]);
    	}

    	M('user')->where(['uid'=>$uid])->save(['password'=>$password]);

    	$this->ajaxReturn(['error'=>0,'info'=>"更新成功"]);

    
    }

    public function mail()
    {

        $str = <<<xbs
<div><table width="800" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#fff" style="font-family: &quot;Microsoft YaHei&quot;;"><tbody><tr><td><table width="800" border="0" align="left" cellpadding="0" cellspacing="0" bgcolor="#373d41" height="48"><tbody><tr><td width="74" height="26" border="0" align="left" valign="middle" style="padding-left:20px"><a href="http://www.alicloud.com/"> <img src="https://g.alicdn.com/aliyun-international/index/0.0.15/images/email-template/cloud-logo-140-20.png" width="140" height="20" border="0"></a> </td></tr></tbody></table></td></tr><tr style="padding:28px 18px;display:inline-block;"><td><table width="763" border="0" align="center" cellpadding="0" cellspacing="0"><tbody><tr><td>只差一步，即可完成阿里云注册！</td></tr><tr style="margin-top:12px;display:block"><td style="color: #666666;font-size: 14px;">请将下方展示的六位验证数字输入进注册验证框，并完成注册。</td></tr><tr style="margin:40px 0;display:block;" align="center"><td style="border:1px solid #A4D3FB;" width="238" valign="middle"><span style="width:35%;display:inline-block;float:left;padding: 10px 0;background:#A4D3FB;color: white;">验证数字</span><span style="width:65%;display:inline-block;float:left;letter-spacing: 5px;padding: 10px 0;color: #1C90F6;">279812</span></td></tr><tr><td width="720" height="14" colspan="2" style="padding-bottom: 16px; border-bottom: 1px dashed rgb(229, 229, 229);">阿里云</td></tr><tr><td width="720" height="14" colspan="2" style="padding: 8px 0px 28px; color: rgb(153, 153, 153); font-size: 12px;">此为系统邮件请勿回复</td></tr></tbody></table></td></tr><tr style="background-color: #F4F4F4;color: #999999;height: 100px;line-height: 100px;padding-left:20px;font-size: 13px;display:block;"><td> Copyrights © AliCloud.com 2009-2017. All rights reserved.&nbsp;</td></tr></tbody></table></div><div><includetail><img src="http://ac.mmstat.com/aliyun.12.1?logtype=4&amp;type=email&amp;msgid=2940217090600003430&amp;areaid=sg&amp;siteId=intl"></includetail></div>
xbs;
       
        sendMail('1034007090@qq.com','注册验证码',$str);
    }
}